package com.jackrain.nea.omslocal.task;

import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.omslocal.service.OrderStatusQueryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Discription: 压测, 自动上传同步订单状态
 * Created with IntelliJ IDEA.
 * @Author cs
 * @Date 2017/10/12 9:07
 */
@Slf4j
@Component
public class AutoUpOrderStatus {

    @Autowired
    private OrderStatusQueryService orderStatusQueryService;

    @Autowired
    private PropertiesConf pconf;

    public void getScheduledExecutor() {
        log.info("AutoMotThreads UpOrdStatus started !!!");
        Runnable runnable = new Runnable() {
            Thread th = Thread.currentThread();

            @Override
            public void run() {
                try {
                    orderStatusQueryService.ordStart();
                    log.debug("任务执行 ==== 线程 ：" + th.getName());
                } catch (Exception e) {
                    e.printStackTrace();
                    // 执行任务错误,将日志记录
                }
            }
        };

        ScheduledExecutorService service = new ScheduledThreadPoolExecutor(5,
                new BasicThreadFactory.Builder().namingPattern("autotaskyace-pool-%d").daemon(true).build());
        // 第二个参数为首次执行的延时时间，第三个参数为定时执行的间隔时间
        // 正式 要延迟适当时间,以便时间初始化赋值
        service.scheduleAtFixedRate(runnable, 10, pconf.getPropertyInt("ons.ordtime"), TimeUnit.SECONDS);
    }


}
